package com.red.wood.module.api.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.Page;
import com.red.wood.module.api.service.ApiService;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/api")
public class ApiController {

	private static final Log log = LogFactory.getLog(ApiController.class);
	
	@Value("${interface.url}")
	private String url;
	
	@Autowired
	private ApiService service;
	
	/**
	 * API分类列表
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("docs");
		return mv;
	}
	
	/**
	 * API分类
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/getApiCategoryInfo")
	public String getApiCategoryInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			json.put("flag",true);
			String categoryNameStr = CommonUtils.valueOf(request.getSession().getAttribute("categoryNameStr")) ;
			json.put("datas", service.getApiCategoryInfo(categoryNameStr));
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
		}	
		return json.toString();
	}
	
	/**
	 * API列表
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@PostMapping(value = "/getApiList")
	public String getApiList(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			String serviceIdStr = CommonUtils.valueOf(request.getSession().getAttribute("serviceIdStr"));
			Map<String,Object> param = new HashMap<String,Object>();
			int pageSize = Integer.parseInt(CommonUtils.valueOf(request.getParameter("pageSize")));
			String currentPage = CommonUtils.valueOf(request.getParameter("currentPage"));
			Page page = Page.newBuilder(Integer.parseInt(currentPage),pageSize,null);  
			param.put("page",page);
			param.put("category_name",request.getParameter("category_name"));
			param.put("serviceIdStr", serviceIdStr);
			Map<String,Object> rs = service.getPageData(param);
			page.setTotalRecord(Integer.parseInt(CommonUtils.valueOf(rs.get("totalCount"))));
			List<Map<String,Object>> list = (List<Map<String, Object>>) rs.get("pageData");
			
			String is_public = CommonUtils.valueOf(request.getSession().getAttribute("is_public"));
			String user = CommonUtils.valueOf(request.getSession().getAttribute("user"));
			boolean flag = false;
			if(StringUtils.isNotBlank(is_public) && StringUtils.isBlank(user)) {
				flag = true;//表示API服务访问的是发布状态
			}else if(StringUtils.isBlank(is_public) && StringUtils.isBlank(user)) {
				flag = true;//表示直接访问的URL
			}
			json.put("flag", true);
			json.put("datas",list);
			json.put("url",url);
			json.put("totalPages",page.getTotalPage());
			json.put("totalData", page.getTotalRecord());
			json.put("currentPage", currentPage);
			json.put("is_public", flag);
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
		}
		return json.toString();
	}
	
	/**
	 * 接口请求参数描述
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/getApiRequestParametersInfo")
	public String getApiRequestParametersInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			String serviceId = CommonUtils.valueOf(request.getParameter("service_id"));
			json.put("flag",true);
			json.put("datas", service.getApiRequestParametersInfo(serviceId));
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
		}	
		return json.toString();
	}
	
	/**
	 * 接口返回参数描述
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/getApiReturnParametersInfo")
	public String getApiReturnParametersInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			String serviceId = CommonUtils.valueOf(request.getParameter("service_id"));
			json.put("flag",true);
			json.put("datas", service.getApiReturnParametersInfo(serviceId));
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
		}	
		return json.toString();
	}
	
	/**
	 * API服务发布
	 * @param request
	 * @param response
	 * @return
	 */
	@PostMapping(value = "/apiServicePublic")
	public String apiServicePublic(HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		try {
			String url = service.apiServicePublic(request);
			json.put("url",url);
			json.put("flag",true);
		}catch(Exception e) {
			if(log.isErrorEnabled()) {
				log.error(e.getMessage(),e);
			}
			json.put("flag",false);
		}	
		return json.toString();
	}
}
